<!DOCTYPE html>
<html>
<head>
   <!-- 页面meta -->
   <meta charset="utf-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <title>传智健康</title>
   <meta name="description" content="传智健康">
   <meta name="keywords" content="传智健康">
   <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
   <!-- 引入样式 -->
   <link rel="stylesheet" href="../plugins/elementui/index.css">
   <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
   <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
   <div class="content-header">
      <h1>系统设置
         <small>权限设置</small>
      </h1>
      <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
         <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
         <el-breadcrumb-item>系统设置</el-breadcrumb-item>
         <el-breadcrumb-item>权限设置</el-breadcrumb-item>
      </el-breadcrumb>
   </div>
   <div class="app-container">
      <div class="box">
         <!--搜索框-->
         <div class="filter-container">
            <el-input placeholder="项目编码/项目名称" v-model="pagination.queryString" style="width: 200px;"
                      class="filter-item"></el-input>
            <el-button @click="findPage()" class="dalfBut">查询</el-button>
            <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
         </div>
         <!--搜索框/-->

         <!--分页数据-->
         <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
            <el-table-column type="id" align="center" label="序号"></el-table-column>
            <el-table-column prop="name" label="权限组名称" align="center"></el-table-column>
            <el-table-column prop="date" label="创建时间" align="center"></el-table-column>
            <el-table-column label="是否启用" align="center">
               <template slot-scope="scope">
                  <!--理解:scope.row为固定写法,表示当前行对象,.sex为获取sex属性-->
                  <span>{{ scope.row.sex == '0' ? '不限' : scope.row.sex == '1' ? '是' : '否'}}</span>
               </template>
            </el-table-column>
            <el-table-column label="操作" align="center">
               <template slot-scope="scope">
                  <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                  <!--scope.row代表当前行的对象-->
                  <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
               </template>
            </el-table-column>
         </el-table>
         <!--分页数据/-->

         <!--分页条-->
         <div class="pagination-container">
            <el-pagination
               class="pagiantion"
               @current-change="handleCurrentChange"
               :current-page="pagination.currentPage"
               :page-size="pagination.pageSize"
               layout="total, prev, pager, next, jumper"
               :total="pagination.total">
            </el-pagination>
         </div>
         <!--分页条/-->

         <!-- 新增标签弹层 -->
         <div  class="add-form"><!--v-if="dialogFormVisible" :visible.sync="dialogFormVisible"-->
            <el-dialog v-if="dialogFormVisible" :visible.sync="dialogFormVisible" title="新增权限组">
               <el-container>
                  <el-aside style="margin-right: 5px" width="150px">

                        <el-input v-model="input" placeholder="请输入权限组名称"></el-input>
                        是否禁用:<el-checkbox v-model="status"></el-checkbox>



                  </el-aside>
                  <el-main style="padding: 0;border-left: 1px solid #8c939d">
                     <div class="checkScrol">
                        <table style="width: 100%" class="datatable">
                           <thead>
                           <tr style="text-align: center">
                              <th  style="width:10%">序号</th>
                              <th style="width:30%">菜单名称</th>
                              <th style="width:60%">权限列表</th>
                           </tr>
                           </thead>
                           <tbody>
                           <tr style="text-align: center" v-for="(c,index) in tableData">
                              <td><label >{{index+1}}</label></td>
                              <td><label :for="c.name">{{c.name}}</label></td>
                              <td><label :for="c.list">
                                 <el-checkbox v-model="c.list.add">新增</el-checkbox>
                                 <el-checkbox v-model="c.list.del">删除</el-checkbox>
                                 <el-checkbox v-model="c.list.upd">修改</el-checkbox>
                                 <el-checkbox v-model="c.list.que">查询</el-checkbox>
                              </label></td>

                           </tr>

                           </tbody>
                        </table>
                     </div>

                  </el-main>
               </el-container>




               <div slot="footer" class="dialog-footer">
                  <!--<el-button @click="dialogFormVisible=false">取消</el-button>-->
                  <el-button @click="dialogFormVisible=false">取消</el-button>
                  <el-button type="primary" @click="handleAdd()">确定</el-button>
               </div>
            </el-dialog>
         </div>

         <!-- 编辑标签弹层 -->
         <div class="add-form">
            <el-dialog v-if="dialogFormVisible4Edit" title="编辑检查项" :visible.sync="dialogFormVisible4Edit">
               <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                  <el-row>
                     <el-col :span="12">
                        <el-form-item label="项目编码" prop="code">
                           <el-input v-model="formData.code"/>
                        </el-form-item>
                     </el-col>
                     <el-col :span="12">
                        <el-form-item label="项目名称" prop="name">
                           <el-input v-model="formData.name"/>
                        </el-form-item>
                     </el-col>
                  </el-row>
                  <el-row>
                     <el-col :span="12">
                        <el-form-item label="性别">
                           <el-select v-model="formData.sex">
                              <el-option label="不限" value="0"></el-option>
                              <el-option label="男" value="1"></el-option>
                              <el-option label="女" value="2"></el-option>
                           </el-select>
                        </el-form-item>
                     </el-col>
                     <el-col :span="12">
                        <el-form-item label="适用年龄">
                           <el-input v-model="formData.age"/>
                        </el-form-item>
                     </el-col>
                  </el-row>
                  <el-row>
                     <el-col :span="12">
                        <el-form-item label="类型">
                           <el-select v-model="formData.type">
                              <el-option label="检查" value="1"></el-option>
                              <el-option label="检验" value="2"></el-option>
                           </el-select>
                        </el-form-item>
                     </el-col>
                     <el-col :span="12">
                        <el-form-item label="价格">
                           <el-input v-model="formData.price"/>
                        </el-form-item>
                     </el-col>
                  </el-row>
                  <el-row>
                     <el-col :span="24">
                        <el-form-item label="项目说明">
                           <el-input v-model="formData.remark" type="textarea"></el-input>
                        </el-form-item>
                     </el-col>
                  </el-row>
                  <el-row>
                     <el-col :span="24">
                        <el-form-item label="注意事项">
                           <el-input v-model="formData.attention" type="textarea"></el-input>
                        </el-form-item>
                     </el-col>
                  </el-row>
               </el-form>
               <div slot="footer" class="dialog-footer">
                  <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                  <el-button type="primary" @click="handleEdit()">确定</el-button>
               </div>
            </el-dialog>
         </div>
      </div>
   </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
   var app = new Vue({
      el: '#app',
      data: {

          tableData:[
              {
                  name:"检查项",
                  list:{
                     add:true,
                      del:true,
                      upd:false,
                      que:true,
                  }
              }, {

                  name:"检查组",
                  list:{

                  }
              }

          ],
          input:"",
          status:"",




         showDialog: true, //弹框是否为新表单
         pagination: {//分页相关模型数据
            currentPage: 1,//当前页码
            pageSize: 10,//每页显示的记录数
            total: 0,//总记录数
            queryString: null//查询条件
         },
         dataList: [],//当前页要展示的分页列表数据
         formData: {},//表单数据
         dialogFormVisible: false,//增加表单是否可见
         dialogFormVisible4Edit: false,//编辑表单是否可见
         rules: {//校验规则
            code: [{required: true, message: '项目编码为必填项', trigger: 'blur'}],
            name: [
               {required: true, message: '请输入名字', trigger: 'blur'}
               /*{min: 1, max: 5, message: '长度在 1 到 5 个字符', trigger: 'blur'}*/
            ]
            /*code: [{required: true, message: '项目编码为必填项', trigger: 'blur'}],
            name: [{required: true, message: '项目名称为必填项', trigger: 'blur'}]*/
         }
      },

      //钩子函数，VUE对象初始化完成后自动执行,
      created() {
         //初始化分页查询
         this.findPage();

      },

      methods: {
         //编辑
         handleEdit() {
            //1.执行表单校验
            this.$refs['dataEditForm'].validate((valid) => {
               if (valid) {//校验成功
                  //2.发送ajax请求
                  axios.post("/checkItem/update.do",this.formData).then((res)=>{
                     //2.1隐藏表单
                     this.dialogFormVisible4Edit = false;

                     if (res.data.flag) { //修改成功
                        //2.3成功提示框
                        this.$message({
                           message: res.data.message,
                           type: 'success'
                        });

                     } else { //修改失败
                        //失败提示框
                        this.$message.error(res.data.message);
                     }

                  }).catch((err) => {
                     this.showMessage(err);
                  }).finally(()=>{
                     //此处不能重置当前页：用户体验，修改后可以立马定位改的位置
                     //2.2分页查询
                     this.findPage(1); //不重置当前页
                  });
               }else{//表单校验失败

                  this.$message.error("表单数据校验失败");
                  return false;
               }
            });
         },

         //添加
         handleAdd() {
            //理解$refs:参数:指定form的ref属性值
            this.$refs['dataAddForm'].validate((valid) => {
               //alert(valid);//测试
               if (valid) {//校验成功
                  //隐藏添加表单,this可以指代vue对象
                  this.dialogFormVisible = false;//隐藏

                  //发送ajax请求
                  axios.post('/checkItem/add.do', this.formData).then(res => {
                     //处理结果{flag:true,message:'hello',data:{..}}
                     //console.log(res.data);//测试
                     if (res.data.flag) {//添加成功

                        //成功弹框
                        this.$message({
                           message: res.data.message,
                           type: 'success'
                        });
                        //调用分页查询
                        this.findPage();
                     } else {//添加失败
                        //失败弹框:使用后台的数据填充
                        this.$message.error(res.data.message)
                     }
                  }).catch(err => { //报错执行
                     this.showMessage(err);
                  });

               } else {//校验失败
                  //弹框
                  this.$message.error('表单数据校验失败');
                  return false;
               }
            });
         },

         //分页查询
         findPage(isCustom) { //参数:表示是否定制当前页
            //debugger;
            //0.判断是从查询按钮调用此方法
            if (!isCustom) {//无惨:默认重置当前页
               //将当前页码重置为1
               this.pagination.currentPage = 1;
            }

            //console.log("hello findPage!");//测试
            //1.封装参数,当前页,每页尺寸,查询条件
            var para = {
               currentPage: this.pagination.currentPage,
               pageSize: this.pagination.pageSize,
               queryString: this.pagination.queryString
            };


            //2.发送ajax请求
            axios.post("/checkItem/findPage.do", para).then(res => {
               //获取返回结果{total:100,rows:[行数据]},赋值给分页模型
               this.pagination.total = res.data.total;
               this.dataList = res.data.rows;//赋值表数据
            }).catch(err => {
               console.log(err);
            }).finally();
         },



         // 重置表单
         resetForm() {
            //清除验证信息
            /*if (this.$refs['dataAddForm'] != undefined) {
              this.$refs['dataAddForm'].resetFields();
            }*/
            //清除验证信息
            /*if (this.$refs['dataAddForm'] != undefined) {
              this.$refs['dataAddForm'].clearValidate();
            }*/
          //  this.tableData = [];
         },

         // 弹出添加窗口
         handleCreate() {
            //console.log("create");//测试
           // this.resetForm();
            this.dialogFormVisible = true;
            //发送axios

             axios.post("/permissionGroup/add.do").then(resp=>{




             })



         },

         // 弹出编辑窗口
         handleUpdate(row) {//不使用row直接回显,可能其他管理员已经删除了此条数据,故以数据库为准
            //1.设置显示编辑窗口
            this.resetForm();
            this.dialogFormVisible4Edit = true;

            //2.发送ajax,回显数据
            axios.get("/checkItem/findById/" + row.id + ".do").then((res) => {
               if (res.data.flag) {//查询成功
                  //设置回显数据
                  this.formData = res.data.data;

               } else {//回显失败原因:多线程同时操作,页面数据已经不存在,故无法回显
                  this.$message.error("获取数据失败,请刷新当前页面")
               }
            }).catch().finally();
         },

         //切换页码
         handleCurrentChange(currentPage) {//当前页更改时回调函数
            //currentPage为新的当前页
            //1.修改当前页数据
            this.pagination.currentPage = currentPage;
            //2.调用分页查询方法
            this.findPage(1); //使用自定义当前页
         },

         // 删除
         handleDelete(row) {//row为当前行json对象

            //确认框
            this.$confirm('确认删除吗?', '提示', {
               confirmButtonText: '确定',
               cancelButtonText: '取消',
               type: 'warning'
            }).then(() => {//点确认,回调
               //发送ajax请求
               axios.delete("/checkItem/delete/" + row.id + ".do").then((res) => {
                  console.log(res.data);//测试
                  //判断是否删除成功
                  if (res.data.flag) {//删除成功
                     //成功框
                     app.$message({
                        message: res.data.message,
                        type: 'success'
                     });

                  } else {//删除失败
                     //失败框
                     this.$message.error(res.data.message);
                  }
               }).catch((err) => {//报错,打印日志
                  console.log(err);
                  //调用方法显示错误信息
                  this.showMessage(err);
               }).finally(() => {//始终执行

                  this.findPage(1);//调用分页查询

               });

            }).catch(() => {//点取消,回调
               //取消框
               this.$message({
                  type: 'info',
                  message: '已取消操作'
               });
            });
         },

         //显示报错时的信息
         showMessage(err) {
            if (err == "Error: Request failed with status code 403") {
               this.$message.error("无访问权限");
            }else {
               this.$message.error("未知错误");
            }
         }

      }
   });
</script>

</html>